package com.jiang.booksystem.manager;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.jiang.booksystem.core.ApiResponse;
import com.jiang.booksystem.core.UserTokenContainer;
import com.jiang.booksystem.dao.entity.Book;
import com.jiang.booksystem.dao.entity.User;
import com.jiang.booksystem.dao.entity.UserBookBorrow;
import com.jiang.booksystem.dao.entity.dos.UserBookBorrowAggre;
import com.jiang.booksystem.dao.mapper.UserBookBorrowMapper;
import com.jiang.booksystem.dao.mapper.UserMapper;
import com.jiang.booksystem.exception.ServiceException;
import com.jiang.booksystem.utils.AssertUtils;
import com.jiang.booksystem.utils.EncryptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserManager {

    @Autowired
    private UserMapper mapper;
    @Autowired
    private UserBookBorrowMapper userBookBorrowMapper;


    public List<User> search(Integer id) {
        return mapper.search(id);
    }

    public User getByUserId(Integer id) {
        User user = mapper.getById(id);
        return user;
    }


    public List<UserBookBorrowAggre> searchApply(int userId) {
        return userBookBorrowMapper.searchByUserId(userId);
    }

    public void add(User user) throws Exception {
        checkUserAddParams(user);
        checkDuplicateName(user);
        mapper.insert(user);
    }

    //登录
    public String signIn(String name, String password) {
        User user = mapper.getByName(name);
        if (!EncryptionUtils.md5(password).equals(user.getPassword())) {
            throw new ServiceException("密码错误");
        } else {
            // 登录成功，返回用户令牌, 用于标识当前用户
            String token = UUID.randomUUID().toString();
            UserTokenContainer.store(token, user);
            return token;
        }
    }

    public void update(String name, String sex, int age) {
        User user = mapper.getByName(name);
        user.setAge(age);
        user.setSex(sex);
        mapper.update(user);
    }

    //还书
    public void returnBooks(int userId, int bookId) {
        List<UserBookBorrow> borrows = userBookBorrowMapper.listByUserIdAndBookId(userId, bookId);

        if (borrows != null) {
            for (UserBookBorrow borrow : borrows) {
                if (borrow.getStatus() == 3) {
                    throw new ServiceException("已经归过还书本");
                } else {
                    UserBookBorrow userBookBorrow = new UserBookBorrow();
                    userBookBorrow.setUserId(userId);
                    userBookBorrow.setBookId(bookId);
                    userBookBorrow.setStatus(3);
                    userBookBorrow.setCreateTime(new Date());

                    userBookBorrowMapper.returnBooks(userBookBorrow);


                }
            }

        }


    }

    //修改密码
    public void password(String name, String password) {
        User user = mapper.getByName(name);

        user.setPassword(EncryptionUtils.md5(password));


        mapper.update(user);
    }


    public void delete(String name) {
        mapper.delete(name);
    }

    private void checkDuplicateName(User user) throws Exception {
        User dbuser = mapper.getByName(user.getName());
        if (dbuser != null) {
            throw new Exception("名字不能重复");
        }
    }

    private void checkUserAddParams(User user) throws Exception {

        AssertUtils.notBlank(user.getName(), "名字不能为空");
        AssertUtils.notBlank(user.getSex(), "性别不能为空");
        AssertUtils.notNull(user.getAge(), "价格不能为空");

        AssertUtils.gt(user.getName(), 10, "名字少与10个字");

//        if (user.getName()==null|| user.getName().equals(""))
//            throw new Exception("名字不能为空");
//        if (user.getSex()==null|| user.getSex().equals(""))
//            throw new Exception("性别不能为空");
//        if (user.getAge()==0)
//            throw new Exception("价格不能为空");
//
//        if (user.getName().length()>10)
//            throw new Exception("名字少与10个字");

    }
}



